package com.pkk.penguin.frame.know.boot.view.controller.hbsl;

import static com.pkk.penguin.frame.know.boot.config.handle.hbsl.HbslClassContent.CLASS_CONTAINER;
import static com.pkk.penguin.frame.know.boot.config.handle.hbsl.HbslClassContent.CLASS_STUDY_COUNT;
import static com.pkk.penguin.frame.know.boot.config.handle.hbsl.HbslClassContent.CURRENT_COOKIE;
import static com.pkk.penguin.frame.know.boot.config.handle.hbsl.HbslClassContent.DEFAULT_STOP_NUM;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.fastjson.JSONObject;
import com.pkk.components.rpc.request.CommonRequest;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import com.pkk.penguin.frame.know.boot.bo.hbsl.MessageType;
import com.pkk.penguin.frame.know.boot.config.common.handle.AbstractWebSocketServer.WebScoketUtil;
import com.pkk.penguin.frame.know.boot.config.handle.hbsl.HbslClassContent;
import com.pkk.penguin.frame.know.boot.config.handle.hbsl.HbslWebSocketServer;
import com.pkk.penguin.frame.know.boot.service.hbsl.HbslStudyService;
import java.nio.charset.Charset;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 华北水利小应用控制器
 * @author: peikunkun
 * @create: 2020-05-12 14:07
 **/
@Slf4j
@RestController
@RequestMapping("hbsl")
public class HbslAppletController {

  @Resource
  private HbslStudyService hbslStudyService;
  @Resource
  private HbslWebSocketServer hbslWebSocketServer;


  @RequestMapping("hbsl")
  public CommonResponse<JSONObject> hbsl(@RequestBody(required = false) CommonRequest<JSONObject> request) {
    return ResponseUtil.success();
  }


  /**
   * @Description: 添加课程
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<com.alibaba.fastjson.JSONObject>
   * @Author: peikunkun
   * @Date: 2020/5/13 0013 下午 2:54
   */
  @RequestMapping("addClass")
  public CommonResponse<Boolean> addClass(@RequestBody CommonRequest<JSONObject> request) {
    try {
      final JSONObject body = request.getBody();
      final String classcode = body.getString("classcode");
      final String classdesc = body.getString("classdesc");
      HbslClassContent.CLASS_CONTAINER.put(classcode, classdesc);
    } catch (Exception e) {
      return ResponseUtil.success(false);
    }
    return ResponseUtil.success(true);
  }

  /**
   * @Description: 更新学习参数
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<com.alibaba.fastjson.JSONObject>
   * @Author: peikunkun
   * @Date: 2020/5/13 0013 下午 3:52
   */
  @RequestMapping("updateStudyParam")
  public CommonResponse<JSONObject> updateStudyParam(@RequestBody CommonRequest<JSONObject> request) {
    final JSONObject body = request.getBody();
    final String cookie = body.getString("cookie");
    final String studyNum = body.getString("studyNum");
    if (StringUtils.isNotBlank(cookie)) {
      CURRENT_COOKIE = cookie;
      WebScoketUtil.broadcost(hbslWebSocketServer.getWebSocket(), MessageType.CONNECTION,
          "设置全局COOKIE值为[" + CURRENT_COOKIE + "]成功\n");
    }
    if (StringUtils.isNotBlank(studyNum) && Integer.valueOf(studyNum) > 0) {
      DEFAULT_STOP_NUM = Integer.valueOf(studyNum);
      WebScoketUtil.broadcost(hbslWebSocketServer.getWebSocket(), MessageType.CONNECTION,
          "设置全局学习次数为[" + DEFAULT_STOP_NUM + "]成功\n");
    }
    return ResponseUtil.success();
  }


  /**
   * @Description: 是否连接的
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/5/13 0013 下午 4:02
   */
  @RequestMapping("connected")
  public CommonResponse<Boolean> connected(@RequestBody CommonRequest<JSONObject> request) {
    final JSONObject body = request.getBody();
    final String clientCode = body.getString("clientCode");
    if (StringUtils.isBlank(clientCode)) {
      return ResponseUtil.success(false);
    }
    return ResponseUtil.success(hbslWebSocketServer.getWebSocket().containsKey(clientCode));
  }


  /**
   * @Description: 正在学习的
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/5/13 0013 下午 4:57
   */
  @RequestMapping("studying")
  public CommonResponse<JSONObject> studying() {
    final JSONObject result = new JSONObject();
    final JSONObject studyInfo = JSONObject.parseObject(JSONObject.toJSONString(CLASS_CONTAINER));
    result.put("课程数量", CLASS_CONTAINER.size());
    result.put("课程信息", studyInfo);
    return ResponseUtil.success(studyInfo);
  }


  /**
   * @Description: 触发学习
   * @return: com.pkk.components.rpc.response.CommonResponse<com.alibaba.fastjson.JSONObject>
   * @Author: peikunkun
   * @Date: 2020/5/13 0013 下午 5:09
   */
  @RequestMapping("triggerStudy")
  public CommonResponse<JSONObject> triggerStudy() {
    hbslStudyService.handleStudy();
    return ResponseUtil.success();
  }

  /**
   * @Description: 放弃学习
   * @return: com.pkk.components.rpc.response.CommonResponse<com.alibaba.fastjson.JSONObject>
   * @Author: peikunkun
   * @Date: 2020/5/13 0013 下午 5:18
   */
  @RequestMapping("giveUpStudy")
  public CommonResponse<JSONObject> giveUpStudy() {
    CLASS_CONTAINER.clear();
    CLASS_STUDY_COUNT.clear();
    return ResponseUtil.success();
  }


  /**
   * @Description: 加载学习课程
   * @return: com.pkk.components.rpc.response.CommonResponse<com.alibaba.fastjson.JSONObject>
   * @Author: peikunkun
   * @Date: 2020/5/13 0013 下午 5:18
   */
  @RequestMapping("loadStudy")
  public CommonResponse<JSONObject> loadStudy() {
    final String classInfoStr = ResourceUtil.readStr("class.txt",Charset.defaultCharset());
    final String[] classArr = classInfoStr.split("\r\n");
    for (int i = 0; i < classArr.length; i++) {
      final String classcode = classArr[i];
      final String classdesc = "default_" + i;
      HbslClassContent.CLASS_CONTAINER.put(classcode, classdesc);
    }
    return ResponseUtil.success();
  }

}
